package net.wofly.finance.expense.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.wofly.finance.expense.domain.ExpenseAccount;
import net.wofly.finance.expense.domain.ExpenseAccountAttachment;
import net.wofly.finance.expense.repository.ExpenseAccountAttachmentRepository;
import net.wofly.finance.expense.service.IExpenseAccountAttachmentService;
import net.wofly.framework.util.Identities;
import net.wofly.framework.util.ReturnBean;

@Service("iExpenseAccountAttachmentService")
@Transactional
public class ExpenseAccountAttachmentServiceImpl implements IExpenseAccountAttachmentService {
	
	@Autowired
	ExpenseAccountAttachmentRepository expenseAccountAttachmentRepository;

	@Override
	public ReturnBean<String> save(ExpenseAccount expenseAccount) {
		
		List<ExpenseAccountAttachment> expenseAccountAttachments = expenseAccount.getExpenseAccountAttachments() ;
		
		if (null == expenseAccountAttachments || expenseAccountAttachments.isEmpty()) {
			return new ReturnBean<>(ReturnBean.CODE_SUCCESS,"保存报销附件成功！") ;
		}
		
		for(ExpenseAccountAttachment expenseAccountAttachment: expenseAccountAttachments) {
			expenseAccountAttachment.setExpenseAccountAttachID(Identities.uuid2());
			expenseAccountAttachment.setExpenseAccountID(expenseAccount.getExpenseAccountID());
			expenseAccountAttachment.setModifyDate(expenseAccount.getModifyDate());
		}
		
		expenseAccountAttachmentRepository.save(expenseAccountAttachments) ;
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS,"保存报销附件成功！") ; 
	}
	
	@Override
	public ReturnBean<String> update(ExpenseAccount expenseAccount) {
		
		expenseAccountAttachmentRepository.deleteByExpenseAccountID(expenseAccount.getExpenseAccountID());
		
		return save(expenseAccount) ;
	}

	@Override
	public List<ExpenseAccountAttachment> findByExpenseAccountID(String expenseAccountID) {
		return expenseAccountAttachmentRepository.findByExpenseAccountID(expenseAccountID);
	}

	
	
	

}
